﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class PermissionConfiguration : IEntityTypeConfiguration<Permission>
    {
        public void Configure(EntityTypeBuilder<Permission> builder)
        {
            // === 基本配置 ===
            builder.ToTable("permissions"); // 表名

            builder.HasKey(p => p.Id); // 主键

            builder.Property(p => p.Id)
                   .IsRequired()
                   .HasMaxLength(36);

            builder.Property(p => p.MenuId)
                   .IsRequired()
                   .HasMaxLength(36);

            builder.Property(p => p.Name)
                   .IsRequired()
                   .HasMaxLength(100)
                   .HasComment("权限名称，例如：新增、删除");

            builder.Property(p => p.Code)
                   .IsRequired()
                   .HasMaxLength(150)
                   .HasComment("权限标识，例如：system:user:add");

            builder.Property(p => p.Sort)
                   .HasDefaultValue(0);

            builder.Property(p => p.IsDeleted)
    .IsRequired()
    .HasDefaultValue(false)
    .HasComment("软删除标志");

            builder.Property(p => p.CreatedAt)
                .IsRequired()
                .HasDefaultValueSql("GETDATE()")
                .HasComment("创建时间");

            builder.Property(p => p.UpdatedAt)
                .HasComment("更新时间");

            // === 关系配置 ===
            builder.HasOne(p => p.Menu)
                   .WithMany(m => m.Permissions)
                   .HasForeignKey(p => p.MenuId)
                   .OnDelete(DeleteBehavior.Cascade);

            // === 索引 ===
            builder.HasIndex(p => p.Code).IsUnique(); // 权限编码唯一
        }
    }
}
